package model;

import java.util.concurrent.Semaphore;

public class ThreadEnvironnement extends Thread {
	/**
	 * Temps en milliseconde de la pause du thread.
	 */
	private long tempsPause = 1000;

	/**
	 * Mettre le thread en pause.
	 */
	private boolean enExecution = false;
	private Semaphore sem = new Semaphore(0);

	public ThreadEnvironnement(Runnable runnable) {
		super(runnable);
	}

	@Override
	public synchronized void start() {
		while (true) {
			try {
				sem.acquire();
				super.run();
				sem.release();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			try {
				sleep(tempsPause);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * @return the tempsPause
	 */
	public long getTempsPause() {
		return tempsPause;
	}

	/**
	 * @param tempsPause
	 *            the tempsPause to set
	 */
	public void setTempsPause(long tempsPause) {
		this.tempsPause = tempsPause;
	}

	/**
	 * @return the enExecution
	 */
	public boolean isEnExecution() {
		return enExecution;
	}

	/**
	 * @param enExecution
	 *            the enExecution to set
	 */
	public void setEnExecution(boolean enExecution) {
		this.enExecution = enExecution;
		if (enExecution) {
			sem.release();
		} else
			try {
				sem.acquire();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	}
}
